package pluginMain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

public class BAKARespawn extends JavaPlugin implements Listener {
	public static BAKARespawn instance;
	public final String PLUGIN_NAME = "RemiliaNetwork Respawn";
	public final Logger CONSOLE_LOGGER = this.getLogger();
	private Configer cfg;
	private ItemStack respawnCoin;
	private Map<String, List<ItemStack>> backpackStorage = new HashMap<>();
	public void LOGCONSOLE(String msg) {
		CONSOLE_LOGGER.info(msg);
	}

	@SuppressWarnings("deprecation")
	public void spawnCoin() {
		cfg = new Configer(this, "config.yml");
		respawnCoin = new ItemStack(Material.getMaterial(cfg.getInt("RespawnCoin.Id")),
				cfg.getInt("RespawnCoin.Amount"), (short) cfg.getInt("RespawnCoin.Data"));
		ItemMeta im = respawnCoin.getItemMeta();
		List<String> lores = new ArrayList<String>(); 
		for (String lore : cfg.getStringList("RespawnCoin.Lores")) {
			lores.add(ChatColor.translateAlternateColorCodes('&', lore));
		}
		im.setLore(lores);
		im.setDisplayName(ChatColor.translateAlternateColorCodes('&', cfg.getString("RespawnCoin.Display")));
		respawnCoin.setItemMeta(im);
	}

	@Override
	public void onEnable() {
		instance = this;
		Configer.CreateDataFoler(this);
		Configer.ReleaseFileIfNotExist(this, "config.yml");
		cfg = new Configer(this, "config.yml");
		spawnCoin();
		getServer().getPluginManager().registerEvents(this, this);
		LOGCONSOLE(PLUGIN_NAME + " has been started.");

	}

	@Override
	public void onDisable() {
		LOGCONSOLE(PLUGIN_NAME + " has been disabled.");
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if (label.equalsIgnoreCase("bres")) {
			if (args[0].equalsIgnoreCase("spawncoin") && sender.hasPermission("bres.spawncoin")) {
				if (sender instanceof Player) {
					((Player) sender).getInventory().addItem(respawnCoin.clone());
					return true;
				} else {
					sender.sendMessage("Only Player could raise coin");
				}
			}
			if(args[0].equalsIgnoreCase("reload") && sender.hasPermission("bres.reload"))
			{
				spawnCoin();
				sender.sendMessage("Reload Complete.");
				return true;
			}
		}
		return false;
	}

	@EventHandler
	public void onPlayerDead(PlayerDeathEvent event) 
	{
		for (ItemStack item : event.getDrops()) {
			if (item.isSimilar(respawnCoin)) {
				List<ItemStack> lStacks = new ArrayList<>();
				lStacks.addAll(event.getDrops());
				
				for (ItemStack itemStack : lStacks) {
					if(itemStack.isSimilar(respawnCoin))
					{
						if(itemStack.getAmount() == 1) lStacks.remove(itemStack);
						else itemStack.setAmount(itemStack.getAmount() - 1);
						break;
					}
				}
				event.getEntity().sendMessage(ChatColor.translateAlternateColorCodes('&', cfg.getString("RespawnInfo.Respawned")));
				event.setKeepInventory(false);
				backpackStorage.put(event.getEntity().getName(), lStacks);
				event.getDrops().clear();
				return;
			}
		}
		event.getEntity().sendMessage(ChatColor.translateAlternateColorCodes('&', cfg.getString("RespawnInfo.NoAny")));
	}

	@EventHandler
	public void onPlayerRespawn(PlayerRespawnEvent event) {
		if (backpackStorage.keySet().contains(event.getPlayer().getName())) {
			for(ItemStack iStack : backpackStorage.get(event.getPlayer().getName()))
			{
				event.getPlayer().getInventory().addItem(iStack);
			}
			backpackStorage.get(event.getPlayer().getName()).clear();
			backpackStorage.remove(event.getPlayer().getName());
		}
	}

}
